package com.mxpio.erp.technology.service.impl;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.technology.entity.ItemDesignFile;
import com.mxpio.erp.common.technology.entity.ItemDesignFileAlter;
import com.mxpio.erp.common.technology.service.ItemDesignFileAlterService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ItemDesignFileAlterServiceImpl extends BaseServiceImpl<ItemDesignFileAlter> implements ItemDesignFileAlterService {
    @Override
    @Transactional
    public Result<ItemDesignFileAlter> auditPass(String alterCode,String result) {
        ItemDesignFileAlter alter = this.getById(ItemDesignFileAlter.class, alterCode);
        if (alter==null){
            return Result.error("未匹配到数据");
        }
        if (StringUtils.equals(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode(),result)){
            String designFileId = alter.getDesignFileId();
            ItemDesignFile file = JpaUtil.getOne(ItemDesignFile.class, designFileId);
            if (file.getSignOffFlag().equals(CommonEnums.YesNo.YES.getCode())){
                file.setSignOffFlag(CommonEnums.YesNo.NO.getCode());
            }
            file.setPrimary(CommonEnums.YesNo.NO.getCode());
            JpaUtil.update(file);

            ItemDesignFile addFile = new ItemDesignFile();
            addFile.setFamily(file.getFamily());
            addFile.setVersion(file.getVersion()+1);
            addFile.setItemCode(file.getItemCode());
            addFile.setFileNo(alter.getFileNo());
            addFile.setFileName(alter.getFileName());
            addFile.setLength(alter.getLength());
            addFile.setExtension(alter.getExtension());
            addFile.setRemarks(alter.getRemarks());
            addFile.setChanger(alter.getCreateBy());
            addFile.setChangeTime(alter.getCreateTime());
            addFile.setChangeCode(alter.getAlterCode());
            JpaUtil.save(addFile);

            if (alter.getEffectiveTime()==null){
                alter.setEffectiveTime(new Date());
            }
            alter.setBpmnStatus( OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode());
        }else{
            alter.setBpmnStatus( OrderStatusEnums.BpmnStatus.CLOSED.getStatusCode());
        }
        update(alter);
        return Result.OK(alter);
    }

    @Override
    @Transactional
    public ItemDesignFileAlter getByIdWith(String alterCode) {
        return JpaUtil.linq(ItemDesignFileAlter.class)
                .collect("itemCode", Item.class,"itemCode")
                .collect("id", ItemDesignFile.class,"designFileId")
                .equal("alterCode",alterCode).findOne();
    }
}
